var categoryFirst = {
	getData : function() {
		$.ajax({
			dataType : "json",
			type : "post",
			url : "/app/category!getAllFirstCategory",
			success : function(data) {
				backUrl.addCookie(data.state);
				var fc = $("#categoryFirst");
				var content = "";
				var fcList = data.msg;
				fc.empty();
				if ('success' == data.state && fcList.length != 0 && fcList != null) {
					content = "<tr id='titleTr'><th scope='col'>序号</th>"
							+ "<th scope='col'>名称</th><th scope='col'>操作</th></tr>";
					for ( var int = 0; int < fcList.length; int++) {
						var v = fcList[int];
						var id = v.first_category_id;
						var name = v.first_category_name;
						var sort = v.sort;
						content += "<tr><td align='center'>" + sort + "</td><td align='center'>" + name
								+ "</td><td align='center'><a href='javascript:updateFC.getData(" + id
								+ ")'>修改</a><a href='javascript:deleteFC.del(" + id + "," + sort
								+ ")'>删除</a><a href='/app/category!jumpSecondCategory?s_id=" + id
								+ "' target='_blank'>查看二级分类</a></td></tr>";
					}
					content += "</dl></div>";
				} else {
					content = "<tr id='noData'><td align='center'><h1>暂无数据</h1></td></tr>";
				}
				fc.append(content);
			}
		});
	}
};
var addFC = {
	TrNumber : function() {
		return $("#categoryFirst").find("tr:not([id='noData'],[id='titleTr'])").size();
	},
	show : function() {
		var num = this.TrNumber() + 1;
		art.dialog({
			lock : true,
			title : '添加一级类别',
			content : "名称：<input type='text' id='addFcName' size='40'/><br>"
					+ "排序：<input type='text' id='addFcSort' size='40' maxlength='4' value='" + num + "'/>",
			width : '25em',
			button : [ {
				name : '添加',
				callback : function() {
					var name = $("#addFcName").val();
					var sort = $("#addFcSort").val();
					addFC.checkValue(name, sort);
				},
				focus : true
			} ],
			cancelVal : '取消',
			cancel : true,
			init : function() {
				$("#addFcName").focus();
			}
		});
	},
	checkValue : function(name, sort) {
		var tof = true;
		var num = addFC.TrNumber;
		for ( var i = 0; i < num; i++) {
			var otherName = $("#categoryFirst").find("tr").eq(i + 1).find("td").eq(1).text();
			if (name == otherName) {
				tof = false;
				errorAlert("分类名称已存在！");
				return false;
			}
		}
		if (tof) {
			num += 1;
			if (sort <= num) {
				var isSort = 1;
				if (sort < num) {
					isSort = 2;
				}
				addFC.add(name, sort, isSort);
			} else {
				$("#addFcSort").val(num);
				return false;
			}
		}
	},
	add : function(name, sort, isSort) {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "name=" + name + "&sort=" + sort + "&isSort=" + isSort,
			url : "/app/category!addFirstCategory",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					categoryFirst.getData();
					tipsAlert(data.msg);

				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	}
};
var updateFC = {
	getData : function(id, sort) {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "f_id=" + id,
			url : "/app/category!getFirstCategoryById",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					var fc = data.msg;
					updateFC.show(fc.first_category_id, fc.first_category_name, fc.sort);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	},
	show : function(id, name, sort) {
		art.dialog({
			id : "addFC",
			lock : true,
			title : '修改一级类别',
			content : "名称：<input type='text' id='updateFcName' size='40' value='" + name + "' beforeName='"
					+ name + "'/><br>"
					+ "排序：<input type='text' id='updateFcSort' size='40' maxlength='4' value='" + sort
					+ "'/>",
			width : '25em',
			button : [ {
				name : '修改',
				callback : function() {
					var name = $("#updateFcName").val();
					var newSort = $("#updateFcSort").val();
					var beforeName = $("#updateFcName").attr("beforeName");
					updateFC.checkValue(id, name, newSort, sort, beforeName);
				},
				focus : true
			} ],
			cancelVal : '取消',
			cancel : true,
			init : function() {
				$("#updateFcName").focus();
			}
		});
	},
	checkValue : function(id, name, newSort, sort, beforeName) {
		var tof = true;
		var num = $("#categoryFirst").find("tr:not([id='titleTr'])").size();
		if (name != beforeName) {
			for ( var i = 0; i < num; i++) {
				var otherName = $("#categoryFirst").find("tr").eq(i + 1).find("td").eq(1).text();
				if (otherName == name) {
					tof = false;
					var msg = "分类名称已存在！";
					errorAlert(msg);
					return false;
				}
			}
		}
		if (tof) {
			num += 1;
			if (newSort <= num) {
				var isSort = 1;
				if (sort != newSort) {// 如果新填写的排序和之前的排序不同，则后台自动排序
					isSort = 2;
				}
				updateFC.update(id, name, newSort, isSort);
			} else {
				$("#updateFcSort").val(num);
				return false;
			}
		}
	},
	update : function(id, name, sort, isSort) {
		$.ajax({
			dataType : "json",
			type : "post",
			async : false,
			data : "f_id=" + id + "&name=" + name + "&sort=" + sort + "&isSort=" + isSort,
			url : "/app/category!updateFirstCategory",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					categoryFirst.getData();
					tipsAlert(data.msg);
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	}
};
deleteFC = {
	del : function(id, sort) {
		art.dialog.confirm('确认删除？', function() {
			var isSort = 2;
			var num = $("#categoryFirst").find("tr:not([id='titleTr'])").size();
			if (num == sort) {
				isSort = 1;
			}
			$.ajax({
				dataType : "json",
				type : "post",
				data : "f_id=" + id + "&isSort=" + isSort,
				url : "/app/category!deleteFristCategory",
				success : function(data) {
					backUrl.addCookie(data.state);
					if (data.state == "success") {
						categoryFirst.getData();
						tipsAlert(data.msg);
					} else if (data.state == "error") {
						errorAlert(data.msg);
					}
				}
			});
		});
	}
};
onloadFC = {
	onload : function() {
		categoryFirst.getData();
		$("#addFC").click(function() {
			addFC.show();
		});
	}
};